/// 训练状态枚举
enum TrainingState {
  idle,    // 空闲
  start,   // 训练中
  pause,   // 暂停
  end,     // 结束
}

/// 训练目标类型枚举
enum GoalsType {
  calories,  // 卡路里目标
  duration,  // 时长目标
  distance,  // 距离目标
}

/// 训练目标信息
class TrainingGoalsInfo {
  final int? caloriesGoal;  // 卡路里目标（kcal）
  final int? durationGoal;  // 时长目标（秒）
  final int? distanceGoal;  // 距离目标（米）

  TrainingGoalsInfo({
    this.caloriesGoal,
    this.durationGoal,
    this.distanceGoal,
  });

  Map<String, dynamic> toJson() {
    return {
      'caloriesGoal': caloriesGoal,
      'durationGoal': durationGoal,
      'distanceGoal': distanceGoal,
    };
  }

  factory TrainingGoalsInfo.fromJson(Map<String, dynamic> json) {
    return TrainingGoalsInfo(
      caloriesGoal: json['caloriesGoal'] as int?,
      durationGoal: json['durationGoal'] as int?,
      distanceGoal: json['distanceGoal'] as int?,
    );
  }
}

/// 历史训练记录
class HistoryTrainingInfo {
  final int id;              // 历史记录序号
  final String type;         // 训练类型
  final int startTime;       // 开始时间戳（毫秒）
  final int endTime;         // 结束时间戳（毫秒）
  final int duration;        // 训练时长（秒）
  final int calories;        // 消耗卡路里（kcal）

  HistoryTrainingInfo({
    required this.id,
    required this.type,
    required this.startTime,
    required this.endTime,
    required this.duration,
    required this.calories,
  });

  factory HistoryTrainingInfo.fromJson(Map<String, dynamic> json) {
    return HistoryTrainingInfo(
      id: json['id'] as int,
      type: json['type'] as String,
      startTime: json['startTime'] as int,
      endTime: json['endTime'] as int,
      duration: json['duration'] as int,
      calories: json['calories'] as int,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'type': type,
      'startTime': startTime,
      'endTime': endTime,
      'duration': duration,
      'calories': calories,
    };
  }

  DateTime get startDateTime => DateTime.fromMillisecondsSinceEpoch(startTime);
  DateTime get endDateTime => DateTime.fromMillisecondsSinceEpoch(endTime);
  
  String get durationFormatted {
    final hours = duration ~/ 3600;
    final minutes = (duration % 3600) ~/ 60;
    final seconds = duration % 60;
    if (hours > 0) {
      return '${hours}h ${minutes}m ${seconds}s';
    } else if (minutes > 0) {
      return '${minutes}m ${seconds}s';
    } else {
      return '${seconds}s';
    }
  }
}

/// 训练详情信息
class TrainingDetailsInfo {
  final int id;              // 训练记录序号
  final String type;         // 训练类型
  final int startTime;       // 开始时间戳（毫秒）
  final int endTime;         // 结束时间戳（毫秒）
  final int duration;        // 训练时长（秒）
  final int calories;        // 消耗卡路里（kcal）
  final int distance;        // 训练距离（米）
  final int avgHeartRate;    // 平均心率（次/分钟）
  final int maxHeartRate;    // 最高心率（次/分钟）
  final bool goalsReached;   // 是否达成训练目标

  TrainingDetailsInfo({
    required this.id,
    required this.type,
    required this.startTime,
    required this.endTime,
    required this.duration,
    required this.calories,
    required this.distance,
    required this.avgHeartRate,
    required this.maxHeartRate,
    required this.goalsReached,
  });

  factory TrainingDetailsInfo.fromJson(Map<String, dynamic> json) {
    return TrainingDetailsInfo(
      id: json['id'] as int,
      type: json['type'] as String,
      startTime: json['startTime'] as int,
      endTime: json['endTime'] as int,
      duration: json['duration'] as int,
      calories: json['calories'] as int,
      distance: json['distance'] as int? ?? 0,
      avgHeartRate: json['avgHeartRate'] as int? ?? 0,
      maxHeartRate: json['maxHeartRate'] as int? ?? 0,
      goalsReached: json['goalsReached'] as bool? ?? false,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'type': type,
      'startTime': startTime,
      'endTime': endTime,
      'duration': duration,
      'calories': calories,
      'distance': distance,
      'avgHeartRate': avgHeartRate,
      'maxHeartRate': maxHeartRate,
      'goalsReached': goalsReached,
    };
  }

  DateTime get startDateTime => DateTime.fromMillisecondsSinceEpoch(startTime);
  DateTime get endDateTime => DateTime.fromMillisecondsSinceEpoch(endTime);
  
  String get durationFormatted {
    final hours = duration ~/ 3600;
    final minutes = (duration % 3600) ~/ 60;
    final seconds = duration % 60;
    if (hours > 0) {
      return '${hours}h ${minutes}m';
    } else {
      return '${minutes}m ${seconds}s';
    }
  }

  String get distanceFormatted {
    if (distance >= 1000) {
      return '${(distance / 1000).toStringAsFixed(2)} km';
    } else {
      return '$distance m';
    }
  }
}
